package com.mpgame.database.mysql.translator;

import com.mpgame.common.proto.GameMonsterStoreInfo;
import com.mpgame.common.proto.GameMonsterStoreSkillInfo;
import com.mpgame.common.utils.Log;
import com.mpgame.database.mysql.model.game.custom.MonsterDisplayInfo;

public class GameMonsterStoreInfoTranslator {
    /**
     * 从MonsterDisplayInfo构建GameMonsterStoreInfo
     * 使用预查询的完整信息，无需额外数据库查询
     */
    public static GameMonsterStoreInfo buildMonsterStoreInfoFromDisplayInfo(MonsterDisplayInfo displayInfo){
        try{
            // 构建技能信息
            GameMonsterStoreSkillInfo skill1 = buildSkillInfoFromDisplayInfo(
                displayInfo.getSkill1Id(), displayInfo.getSkill1Name(), 
                displayInfo.getSkill1Power(), displayInfo.getSkill1Pp(), 
                displayInfo.getSkill1Description());
            
            GameMonsterStoreSkillInfo skill2 = buildSkillInfoFromDisplayInfo(
                displayInfo.getSkill2Id(), displayInfo.getSkill2Name(), 
                displayInfo.getSkill2Power(), displayInfo.getSkill2Pp(), 
                displayInfo.getSkill2Description());
            
            GameMonsterStoreSkillInfo skill3 = buildSkillInfoFromDisplayInfo(
                displayInfo.getSkill3Id(), displayInfo.getSkill3Name(), 
                displayInfo.getSkill3Power(), displayInfo.getSkill3Pp(), 
                displayInfo.getSkill3Description());
            
            GameMonsterStoreSkillInfo skill4 = buildSkillInfoFromDisplayInfo(
                displayInfo.getSkill4Id(), displayInfo.getSkill4Name(), 
                displayInfo.getSkill4Power(), displayInfo.getSkill4Pp(), 
                displayInfo.getSkill4Description());

            // 构建怪物信息
            GameMonsterStoreInfo monsterInfo = GameMonsterStoreInfo.newBuilder()
                .setMonsterUrl("") // 暂时为空，后续可以配置
                .setName(displayInfo.getMonsterName())
                .setFamilyUrl("") // 暂时为空，后续可以配置
                .setLevel(displayInfo.getLv())
                .setWisdom(displayInfo.getWisdom())
                .setMeleeAtk(displayInfo.getMeleeAtk())
                .setMagicAtk(displayInfo.getMagicAtk())
                .setMeleeDef(displayInfo.getMeleeDef())
                .setMagicDef(displayInfo.getMagicDef())
                .setSpeed(displayInfo.getSpeed())
                .setHealth(displayInfo.getHealth())
                .setSkill1(skill1)
                .setSkill2(skill2)
                .setSkill3(skill3)
                .setSkill4(skill4)
                .build();
            
            Log.info("构建怪物信息成功：" + displayInfo.getMonsterName() + " (ID: " + displayInfo.getStoreId() + ")");
            return monsterInfo;
        }catch(Exception e){
            Log.err("构建怪物信息异常：" + e.getMessage());
            return null;
        }
    }

    /**
     * 从MonsterDisplayInfo中的技能信息构建GameMonsterStoreSkillInfo
     */
    private static GameMonsterStoreSkillInfo buildSkillInfoFromDisplayInfo(int skillId, String skillName, 
        int power, int pp, String description){
        try{
            if(skillId <= 0 || skillName == null || skillName.isEmpty()){
                return GameMonsterStoreSkillInfo.newBuilder()
                    .setFamilyUrl("")
                    .setName("")
                    .setPower(0)
                    .setMaxPp(0)
                    .setDescription("")
                    .build();
            }
            
            return GameMonsterStoreSkillInfo.newBuilder()
                .setFamilyUrl("") // 暂时为空，后续可以配置
                .setName(skillName)
                .setPower(power)
                .setMaxPp(pp)
                .setDescription(description != null ? description : "")
                .build();
        }catch(Exception e){
            Log.err("构建技能信息异常：" + e.getMessage());
            return GameMonsterStoreSkillInfo.newBuilder()
                .setFamilyUrl("")
                .setName("")
                .setPower(0)
                .setMaxPp(0)
                .setDescription("")
                .build();
        }
    }
}
